home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Binding / Connection.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  16.2 KB  |  656 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Binding::Connection - A connection between client and server
  24.  
  25. =head1 SYNOPSIS
  26.  
  27. Creating a connection to a server and sending a message
  28.  
  29.   use Net::DBus::Binding::Connection;
  30.  
  31.   my $con = Net::DBus::Binding::Connection->new(address => "unix:path=/path/to/socket");
  32.  
  33.   $con->send($message);
  34.  
  35. Registering message handlers
  36.  
  37.   sub handle_something {
  38.       my $con = shift;
  39.       my $msg = shift;
  40.  
  41.       ... do something with the message...
  42.   }
  43.  
  44.   $con->register_message_handler(
  45.     "/some/object/path",
  46.     \&handle_something);
  47.  
  48. Hooking up to an event loop:
  49.  
  50.   my $reactor = Net::DBus::Binding::Reactor->new();
  51.  
  52.   $reactor->manage($con);
  53.  
  54.   $reactor->run();
  55.  
  56. =head1 DESCRIPTION
  57.  
  58. An outgoing connection to a server, or an incoming connection
  59. from a client. The methods defined on this module have a close
  60. correspondance to the dbus_connection_XXX methods in the C API,
  61. so for further details on their behaviour, the C API documentation
  62. may be of use.
  63.  
  64. =head1 METHODS
  65.  
  66. =over 4
  67.  
  68. =cut
  69.  
  70. package Net::DBus::Binding::Connection;
  71.  
  72. use 5.006;
  73. use strict;
  74. use warnings;
  75.  
  76. use Net::DBus;
  77. use Net::DBus::Binding::Message::MethodCall;
  78. use Net::DBus::Binding::Message::MethodReturn;
  79. use Net::DBus::Binding::Message::Error;
  80. use Net::DBus::Binding::Message::Signal;
  81. use Net::DBus::Binding::PendingCall;
  82.  
  83. =item my $con = Net::DBus::Binding::Connection->new(address => "unix:path=/path/to/socket");
  84.  
  85. Creates a new connection to the remove server specified by
  86. the parameter C<address>. If the C<private> parameter is
  87. supplied, and set to a True value the connection opened is
  88. private; otherwise a shared connection is opened. A private
  89. connection must be explicitly shutdown with the C<disconnect>
  90. method before the last reference to the object is released.
  91. A shared connection must never be explicitly disconnected.
  92.  
  93. =cut
  94.  
  95. sub new {
  96.     my $proto = shift;
  97.     my $class = ref($proto) || $proto;
  98.     my %params = @_;
  99.     my $self = {};
  100.  
  101.     my $private = $params{private} ? $params{private} : 0;
  102.     $self->{address} = exists $params{address} ? $params{address} : (exists $params{connection} ? "" : die "address parameter is required");
  103.     $self->{connection} = exists $params{connection} ? $params{connection} :
  104.     ($private ?
  105.      Net::DBus::Binding::Connection::_open_private($self->{address}) :
  106.      Net::DBus::Binding::Connection::_open($self->{address}));
  107.  
  108.     bless $self, $class;
  109.  
  110.     $self->{connection}->_set_owner($self);
  111.  
  112.     return $self;
  113. }
  114.  
  115.  
  116. =item $status = $con->is_connected();
  117.  
  118. Returns zero if the connection has been disconnected,
  119. otherwise a positive value is returned.
  120.  
  121. =cut
  122.  
  123. sub is_connected {
  124.     my $self = shift;
  125.     
  126.     return $self->{connection}->dbus_connection_get_is_connected();
  127. }
  128.  
  129. =item $status = $con->is_authenticated();
  130.  
  131. Returns zero if the connection has not yet successfully
  132. completed authentication, otherwise a positive value is
  133. returned.
  134.  
  135. =cut
  136.  
  137. sub is_authenticated {
  138.     my $self = shift;
  139.     
  140.     return $self->{connection}->dbus_connection_get_is_authenticated();
  141. }
  142.  
  143.  
  144. =item $con->disconnect()
  145.  
  146. Closes this connection to the remote host. This method
  147. is called automatically during garbage collection (ie
  148. in the DESTROY method) if the programmer forgets to
  149. explicitly disconnect.
  150.  
  151. =cut
  152.  
  153. sub disconnect {
  154.     my $self = shift;
  155.     
  156.     $self->{connection}->dbus_connection_disconnect();
  157. }
  158.  
  159. =item $con->flush()
  160.  
  161. Blocks execution until all data in the outgoing data
  162. stream has been sent. This method will not re-enter
  163. the application event loop.
  164.  
  165. =cut
  166.  
  167. sub flush {
  168.     my $self = shift;
  169.     
  170.     $self->{connection}->dbus_connection_flush();
  171. }
  172.  
  173.  
  174. =item $con->send($message)
  175.  
  176. Queues a message up for sending to the remote host.
  177. The data will be sent asynchronously as the applications
  178. event loop determines there is space in the outgoing 
  179. socket send buffer. To force immediate sending of the
  180. data, follow this method will a call to C<flush>. This
  181. method will return the serial number of the message,
  182. which can be used to identify a subsequent reply (if
  183. any).
  184.  
  185. =cut
  186.  
  187. sub send {
  188.     my $self = shift;
  189.     my $msg = shift;
  190.  
  191.     return $self->{connection}->_send($msg->{message});
  192. }
  193.  
  194. =item my $reply = $con->send_with_reply_and_block($msg, $timeout);
  195.  
  196. Queues a message up for sending to the remote host
  197. and blocks until it has been sent, and a corresponding
  198. reply received. The return value of this method will
  199. be a C<Net::DBus::Binding::Message::MethodReturn> or C<Net::DBus::Binding::Message::Error>
  200. object.
  201.  
  202. =cut
  203.  
  204. sub send_with_reply_and_block {
  205.     my $self = shift;
  206.     my $msg = shift;
  207.     my $timeout = shift;
  208.  
  209.     my $reply = $self->{connection}->_send_with_reply_and_block($msg->{message}, $timeout);
  210.  
  211.     my $type = $reply->dbus_message_get_type;
  212.     if ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
  213.     return $self->make_raw_message($reply);
  214.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN) {
  215.     return $self->make_raw_message($reply);
  216.     } else {
  217.     die "unknown method reply type $type";
  218.     }
  219. }
  220.  
  221.  
  222. =item my $pending_call = $con->send_with_reply($msg, $timeout);
  223.  
  224. Queues a message up for sending to the remote host
  225. and returns immediately providing a reference to a
  226. C<Net::DBus::Binding::PendingCall> object. This object
  227. can be used to wait / watch for a reply. This allows
  228. methods to be processed asynchronously.
  229.  
  230. =cut
  231.  
  232. sub send_with_reply {
  233.     my $self = shift;
  234.     my $msg = shift;
  235.     my $timeout = shift;
  236.  
  237.     my $reply = $self->{connection}->_send_with_reply($msg->{message}, $timeout);
  238.  
  239.     return Net::DBus::Binding::PendingCall->new(connection => $self,
  240.                         method_call => $msg,
  241.                         pending_call => $reply);
  242. }
  243.  
  244.  
  245. =item $con->dispatch;
  246.  
  247. Dispatches any pending messages in the incoming queue
  248. to their message handlers. This method is typically
  249. called on each iteration of the main application event
  250. loop where data has been read from the incoming socket.
  251.  
  252. =cut
  253.  
  254. sub dispatch {
  255.     my $self = shift;
  256.     
  257.     $self->{connection}->_dispatch();
  258. }
  259.  
  260.  
  261. =item $message = $con->borrow_message
  262.  
  263. Temporarily removes the first message from the incoming
  264. message queue. No other thread may access the message
  265. while it is 'borrowed', so it should be replaced in the
  266. queue with the C<return_message> method, or removed 
  267. permanently with th C<steal_message> method as soon as
  268. is practical.
  269.  
  270. =cut
  271.  
  272. sub borrow_message {
  273.     my $self = shift;
  274.     
  275.     my $msg = $self->{connection}->dbus_connection_borrow_message();
  276.     return $self->make_raw_message($msg);
  277. }
  278.  
  279. =item $con->return_message($msg)
  280.  
  281. Replaces a previously borrowed message in the incoming
  282. message queue for subsequent dispatch to registered 
  283. message handlers.
  284.  
  285. =cut
  286.  
  287. sub return_message {
  288.     my $self = shift;
  289.     my $msg = shift;
  290.     
  291.     $self->{connection}->dbus_connection_return_message($msg->{message});
  292. }
  293.  
  294.  
  295. =item $con->steal_message($msg)
  296.  
  297. Permanently remove a borrowed message from the incoming
  298. message queue. No registered message handlers will now
  299. be run for this message.
  300.  
  301. =cut
  302.  
  303. sub steal_message {
  304.     my $self = shift;
  305.     my $msg = shift;
  306.     
  307.     $self->{connection}->dbus_connection_steal_borrowed_message($msg->{message});
  308. }
  309.  
  310. =item $msg = $con->pop_message();
  311.  
  312. Permanently removes the first message on the incoming
  313. message queue, without running any registered message
  314. handlers. If you have hooked the connection up to an
  315. event loop (C<Net::DBus::Binding::Reactor> for example), you probably
  316. don't want to be calling this method.
  317.  
  318. =cut
  319.  
  320. sub pop_message {
  321.     my $self = shift;
  322.     
  323.     my $msg = $self->{connection}->dbus_connection_pop_message();
  324.     return $self->make_raw_message($msg);
  325. }
  326.  
  327. =item $con->set_watch_callbacks(\&add_watch, \&remove_watch, \&toggle_watch);
  328.  
  329. Register a set of callbacks for adding, removing & updating 
  330. watches in the application's event loop. Each parameter
  331. should be a code reference, which on each invocation, will be
  332. supplied with two parameters, the connection object and the
  333. watch object. If you are using a C<Net::DBus::Binding::Reactor> object
  334. as the application event loop, then the 'manage' method on
  335. that object will call this on your behalf.
  336.  
  337. =cut
  338.  
  339. sub set_watch_callbacks {
  340.     my $self = shift;
  341.     my $add = shift;
  342.     my $remove = shift;
  343.     my $toggled = shift;
  344.  
  345.     $self->{add_watch} = $add;
  346.     $self->{remove_watch} = $remove;
  347.     $self->{toggled_watch} = $toggled;
  348.  
  349.     $self->{connection}->_set_watch_callbacks();
  350. }
  351.  
  352. =item $con->set_timeout_callbacks(\&add_timeout, \&remove_timeout, \&toggle_timeout);
  353.  
  354. Register a set of callbacks for adding, removing & updating 
  355. timeouts in the application's event loop. Each parameter
  356. should be a code reference, which on each invocation, will be
  357. supplied with two parameters, the connection object and the
  358. timeout object. If you are using a C<Net::DBus::Binding::Reactor> object
  359. as the application event loop, then the 'manage' method on
  360. that object will call this on your behalf.
  361.  
  362. =cut
  363.  
  364. sub set_timeout_callbacks {
  365.     my $self = shift;
  366.     my $add = shift;
  367.     my $remove = shift;
  368.     my $toggled = shift;
  369.  
  370.     $self->{add_timeout} = $add;
  371.     $self->{remove_timeout} = $remove;
  372.     $self->{toggled_timeout} = $toggled;
  373.  
  374.     $self->{connection}->_set_timeout_callbacks();
  375. }
  376.  
  377. =item $con->register_object_path($path, \&handler)
  378.  
  379. Registers a handler for messages whose path matches
  380. that specified in the C<$path> parameter. The supplied
  381. code reference will be invoked with two parameters, the
  382. connection object on which the message was received,
  383. and the message to be processed (an instance of the
  384. C<Net::DBus::Binding::Message> class).
  385.  
  386. =cut
  387.  
  388. sub register_object_path {
  389.     my $self = shift;
  390.     my $path = shift;
  391.     my $code = shift;
  392.  
  393.     my $callback = sub {
  394.     my $con = shift;
  395.     my $msg = shift;
  396.  
  397.     &$code($con, $self->make_raw_message($msg));
  398.     };
  399.     $self->{connection}->_register_object_path($path, $callback);
  400. }
  401.  
  402. =item $con->unregister_object_path($path)
  403.  
  404. Unregisters the handler associated with the object path C<$path>. The
  405. handler would previously have been registered with the C<register_object_path>
  406. or C<register_fallback> methods.
  407.  
  408. =cut
  409.  
  410. sub unregister_object_path {
  411.     my $self = shift;
  412.     my $path = shift;
  413.     $self->{connection}->_unregister_object_path($path);
  414. }
  415.  
  416.  
  417. =item $con->register_fallback($path, \&handler)
  418.  
  419. Registers a handler for messages whose path starts with 
  420. the prefix specified in the C<$path> parameter. The supplied
  421. code reference will be invoked with two parameters, the
  422. connection object on which the message was received,
  423. and the message to be processed (an instance of the
  424. C<Net::DBus::Binding::Message> class).
  425.  
  426. =cut
  427.  
  428. sub register_fallback {
  429.     my $self = shift;
  430.     my $path = shift;
  431.     my $code = shift;
  432.  
  433.     my $callback = sub {
  434.     my $con = shift;
  435.     my $msg = shift;
  436.  
  437.     &$code($con, $self->make_raw_message($msg));
  438.     };
  439.  
  440.     $self->{connection}->_register_fallback($path, $callback);
  441. }
  442.  
  443.  
  444. =item $con->set_max_message_size($bytes)
  445.  
  446. Sets the maximum allowable size of a single incoming
  447. message. Messages over this size will be rejected
  448. prior to exceeding this threshold. The message size
  449. is specified in bytes.
  450.  
  451. =cut
  452.  
  453. sub set_max_message_size {
  454.     my $self = shift;
  455.     my $size = shift;
  456.     
  457.     $self->{connection}->dbus_connection_set_max_message_size($size);
  458. }
  459.  
  460. =item $bytes = $con->get_max_message_size();
  461.  
  462. Retrieves the maximum allowable incoming
  463. message size. The returned size is measured
  464. in bytes.
  465.  
  466. =cut
  467.  
  468. sub get_max_message_size {
  469.     my $self = shift;
  470.     
  471.     return $self->{connection}->dbus_connection_get_max_message_size;
  472. }
  473.  
  474. =item $con->set_max_received_size($bytes)
  475.  
  476. Sets the maximum size of the incoming message queue.
  477. Once this threashold is exceeded, no more messages will
  478. be read from wire before one or more of the existing
  479. messages are dispatched to their registered handlers.
  480. The implication is that the message queue can exceed
  481. this threshold by at most the size of a single message.
  482.  
  483. =cut
  484.  
  485. sub set_max_received_size {
  486.     my $self = shift;
  487.     my $size = shift;
  488.     
  489.     $self->{connection}->dbus_connection_set_max_received_size($size);
  490. }
  491.  
  492. =item $bytes $con->get_max_received_size()
  493.  
  494. Retrieves the maximum incoming message queue size.
  495. The returned size is measured in bytes.
  496.  
  497. =cut
  498.  
  499. sub get_max_received_size {
  500.     my $self = shift;
  501.     
  502.     return $self->{connection}->dbus_connection_get_max_received_size;
  503. }
  504.  
  505.  
  506. =item $con->add_filter($coderef);
  507.  
  508. Adds a filter to the connection which will be invoked whenever a
  509. message is received. The C<$coderef> should be a reference to a
  510. subroutine, which returns a true value if the message should be
  511. filtered out, or a false value if the normal message dispatch
  512. should be performed.
  513.  
  514. =cut
  515.  
  516. sub add_filter {
  517.     my $self = shift;
  518.     my $callback = shift;
  519.     
  520.     $self->{connection}->_add_filter($callback);
  521. }
  522.  
  523.  
  524. sub _message_filter {
  525.     my $self = shift;
  526.     my $rawmsg = shift;
  527.     my $code = shift;
  528.     
  529.     my $msg = $self->make_raw_message($rawmsg);
  530.     return &$code($self, $msg);
  531. }
  532.  
  533.  
  534. =item my $msg = $con->make_raw_message($rawmsg)
  535.  
  536. Creates a new message, initializing it from the low level C message
  537. object provided by the C<$rawmsg> parameter. The returned object
  538. will be cast to the appropriate subclass of L<Net::DBus::Binding::Message>.
  539.  
  540. =cut
  541.  
  542. sub make_raw_message {
  543.     my $self = shift;
  544.     my $rawmsg = shift;
  545.     
  546.     return Net::DBus::Binding::Message->new(message => $rawmsg);
  547. }
  548.  
  549.  
  550. =item my $msg = $con->make_error_message(
  551.       replyto => $method_call, name => $name, description => $description);
  552.  
  553. Creates a new message, representing an error which occurred during
  554. the handling of the method call object passed in as the C<replyto>
  555. parameter. The C<name> parameter is the formal name of the error
  556. condition, while the C<description> is a short piece of text giving
  557. more specific information on the error.
  558.  
  559. =cut
  560.  
  561.  
  562. sub make_error_message {
  563.     my $self = shift;
  564.     my $replyto = shift;
  565.     my $name = shift;
  566.     my $description = shift;
  567.  
  568.     return Net::DBus::Binding::Message::Error->new(replyto => $replyto,
  569.                            name => $name,
  570.                            description => $description);
  571. }
  572.  
  573. =item my $call = $con->make_method_call_message(
  574.   $service_name, $object_path, $interface, $method_name);
  575.  
  576. Create a message representing a call on the object located at
  577. the path C<$object_path> within the client owning the well-known
  578. name given by C<$service_name>. The method to be invoked has
  579. the name C<$method_name> within the interface specified by the
  580. C<$interface> parameter.
  581.  
  582. =cut
  583.  
  584.  
  585. sub make_method_call_message {
  586.     my $self = shift;
  587.     my $service_name = shift;
  588.     my $object_path = shift;
  589.     my $interface = shift;
  590.     my $method_name = shift;
  591.  
  592.     return Net::DBus::Binding::Message::MethodCall->new(service_name => $service_name,
  593.                             object_path => $object_path,
  594.                             interface => $interface,
  595.                             method_name => $method_name);
  596. }
  597.  
  598. =item my $msg = $con->make_method_return_message(
  599.     replyto => $method_call);
  600.  
  601. Create a message representing a reply to the method call passed in
  602. the C<replyto> parameter.
  603.  
  604. =cut
  605.  
  606.  
  607. sub make_method_return_message {
  608.     my $self = shift;
  609.     my $replyto = shift;
  610.  
  611.     return Net::DBus::Binding::Message::MethodReturn->new(call => $replyto);
  612. }
  613.  
  614.  
  615. =item my $signal = $con->make_signal_message(
  616.       object_path => $path, interface => $interface, signal_name => $name);
  617.  
  618. Creates a new message, representing a signal [to be] emitted by
  619. the object located under the path given by the C<object_path>
  620. parameter. The name of the signal is given by the C<signal_name>
  621. parameter, and is scoped to the interface given by the
  622. C<interface> parameter.
  623.  
  624. =cut
  625.  
  626. sub make_signal_message {
  627.     my $self = shift;
  628.     my $object_path = shift;
  629.     my $interface = shift;
  630.     my $signal_name = shift;
  631.  
  632.     return Net::DBus::Binding::Message::Signal->new(object_path => $object_path,
  633.                             interface => $interface,
  634.                             signal_name => $signal_name);
  635. }
  636.  
  637. 1;
  638.  
  639. =pod
  640.  
  641. =back
  642.  
  643. =head1 SEE ALSO
  644.  
  645. L<Net::DBus::Binding::Server>, L<Net::DBus::Binding::Bus>, L<Net::DBus::Binding::Message::Signal>, L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>, L<Net::DBus::Binding::Message::Error>
  646.  
  647. =head1 AUTHOR
  648.  
  649. Daniel Berrange E<lt>dan@berrange.comE<gt>
  650.  
  651. =head1 COPYRIGHT
  652.  
  653. Copyright 2004 by Daniel Berrange
  654.  
  655. =cut
  656.